<?php


class Application_Model_Form
{
    protected $type;
    protected $model;
    protected $tpl;
    protected $isAddMode;

    private $pathTpl = null;

    const TYPE_TERM = 'term';
    const TYPE_BUY  = 'buy';
    const TYPE_TASK = 'task';
    const TYPE_NOTE = 'note';
    const TYPE_TRAVEL = 'travel';

    public function __construct($id, $type)
    {
        $this->pathTpl = APPLICATION_PATH . '/views/scripts/forms/';

        switch ($type) {
            case self::TYPE_TERM:
                $mapper = new Application_Model_Mapper(new Application_Model_DbTable_Terms(), new Application_Model_Term());
                $this->model = $mapper->getById($id);

                if (!$this->model->getTermName()) {
                    $this->isAddMode = true;
                }

                $this->tpl = 'term.phtml';
                return $this;
            case self::TYPE_BUY:
                $mapper = new Application_Model_Mapper(new Application_Model_DbTable_Buy(), new Application_Model_Buy());
                $this->model = $mapper->getById($id);

                if (!$this->model->getBuyName()) {
                    $this->isAddMode = true;
                }

                $this->tpl = 'buy.phtml';
                return $this;
            case self::TYPE_TASK:
                $mapper = new Application_Model_Mapper(new Application_Model_DbTable_Tasks(), new Application_Model_Task());
                $this->model = $mapper->getById($id);
                if (!$this->model->getTaskTitle()) {
                    $this->isAddMode = true;
                }

                $this->tpl = 'idea.phtml';
                return $this;
            case self::TYPE_TRAVEL:
                $mapper = new Application_Model_Mapper(new Application_Model_DbTable_Travels(), new Application_Model_Travel());
                $this->model = $mapper->getById($id);

                if (!$this->model->getTitle()) {
                    $this->isAddMode = true;
                }

                $this->tpl = 'travel.phtml';
                return $this;
            case self::TYPE_NOTE:
                $mapper = new Application_Model_Mapper(new Application_Model_DbTable_Notes(), new Application_Model_Note());
                $this->model = $mapper->getById($id);

                if (!$this->model->getNoteTitle()) {
                    $this->isAddMode = true;
                }

                $this->tpl = 'note.phtml';
                return $this;
        }

        return null;
    }

    public function render()
    {
        $view = new Zend_View();
        $view->setScriptPath($this->pathTpl);
        $view->isAddMode = $this->isAddMode;
        $view->model = $this->model;
        return $view->render($this->tpl);
    }

}